package com.sunnada.edu.system.login.controller;




/**
 * <p>Title: 生成校验码</p>
 * <p>Description: 生成图象单元.取得图象码</p>
 * <p>Copyright: sunnada softteam Copyright (c) 2006</p>
 * <p>Company: sunnada.com</p>
 * @author hjw
 * @version 1.0
 */

import java.awt.*;
import java.awt.image.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageCode {

  private String sRand = "";

  //给定范围获得随机颜色
  private Color getRandColor(int fc, int bc)
  {
      Random random = new Random();
      if (fc > 255) { 
          fc = 255;
      }
      if (bc > 255) {
          bc = 255;
      }
      int d=bc - fc;
      int r = fc + random.nextInt(d);
      int g = fc + random.nextInt(d);
      int b = fc + random.nextInt(d);
      return new Color(r, g, b);
  }

  // 在内存中创建图象
  public BufferedImage creatImage()
  {
      int width = 63, height = 22;
      BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
      // 获取图形上下文
      Graphics g = image.getGraphics();
      Random random = new Random();
      //设定背景色
      g.setColor(getRandColor(220, 250));
      g.fillRect(0, 0, width, height);

      g.setColor(new Color(0x7ac4ea));
      g.drawRect(0,0,width-1,height-1);
      //设定字体,颜色与背景区别开

      g.setFont(new Font("Times New Roman", Font.BOLD, 20));
      g.setColor(getRandColor(160, 220));
      // 随机产生155条干扰线，
      for (int i = 0; i < 20; i++)
      {
          int x = random.nextInt(width);
          int y = random.nextInt(height);
          int xl = random.nextInt(12);
          int yl = random.nextInt(12);
          g.drawLine(x, y, x + xl, y + yl);
      }
      // 取随机产生的认证码(4位数字)
      StringBuffer s = new StringBuffer(36);
      //s.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890");
      s.append("1234567890");
      for (int i = 0; i < 4; i++) {
          //char cc = s.charAt(random.nextInt(62));
          char cc = s.charAt(random.nextInt(10));
          String rand = String.valueOf(cc);
          sRand += rand;
          // 将认证码显示到图象中
          g.setColor(new Color(20 + random.nextInt(110),
                               20 + random.nextInt(110),
                               20 + random.nextInt(110)));
          g.drawString(rand, 13*i+6,16);
      }
      // 图象生效
      g.dispose();
      return image;
  }
  
  
	/**
	 * 将图片转换为输入流
	 */
	public  InputStream getInputStream(BufferedImage image)
			throws IOException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
		encoder.encode(image);
		byte[] imageBts = bos.toByteArray();
		InputStream in = new ByteArrayInputStream(imageBts);
		return in;
	}

  /**
   * @return Returns the sRand.
   */
  public String getSRand() {
      return sRand;
  }
  public static void main(String[] args){

  }
}
